﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.Persistent;
using Jumpton.SFA.Entity;
public partial class Message_Maintain : PageBase
{
    #region 定义变量
    private const string NEW_PAGE = "Message_Manager.aspx";
    private const string SUCCESS = "操作成功！";
    private const string ERROR = "操作失败！";
    private MessageManager messManager = null;  //消息作类
    private Message message = null;              //消息对象
    
   
    #endregion

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //DetailsView1.EnableDynamicData(typeof(Message));
 
        if (!IsPostBack)
        {
       
             BindMessageLevel();
             txtStartTime.Text = txtEndTime.Text= DateTime.Now.ToString("yyyy-MM-dd");
           
        }

    }


    protected void BindMessageLevel()
    {
        DictionaryItemsManager dim = new DictionaryItemsManager();
        ddlMessageLevel.DataTextField = "Name";
        ddlMessageLevel.DataValueField = "DictionaryItemId";

        ddlMessageLevel.DataSource = dim.GetItemList(DictionaryEnum.MessageType, this.MyStateManager.DomainID);
        ddlMessageLevel.DataBind();
       

    }

  

    /// <summary>
    /// 提交数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string LogContent = "";
        try
        {

            if (!UploadImage())
            {
                ShowMessage("不是有效的图片文件！");
                return;
            }

            message = new Message();
            message.CREATETIME = long.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
            message.TOPIC = txtTopic.Text;
            message.BEGINDATE =long.Parse(DateTime.Parse(txtStartTime.Text).ToString("yyyyMMdd"));
            message.ENDDATE = long.Parse(DateTime.Parse(txtEndTime.Text).ToString("yyyyMMdd"));
            message.DOMAINID = int.Parse(MyStateManager.DomainID.ToString());
            message.CONTENTLEVEL = int.Parse(ddlMessageLevel.SelectedValue);
            message.LAST_UPDATER = MyStateManager.Name;
            message.LAST_UPDATE_TIME = long.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
            message.CONTENT = txtContent.Text;
            message.ACTIVE = chxActive.Checked;
            message.RECEIVERCOUNT = int.Parse(txtCount.Value);
            message.READCOUNT = 0;
           
            LogContent += "消息主题：" + message.TOPIC ;
            LogContent += "消息开始日期：" + message.BEGINDATE;
            LogContent += "消息结束日期：" + message.ENDDATE;
            LogContent += "消息类型：" + message.CONTENTLEVEL;
            LogContent += "更新时间：" + message.LAST_UPDATE_TIME;
            LogContent += "更新人：" + message.LAST_UPDATER;
            LogContent += "是否有效：" + message.ACTIVE;
            LogContent += "消息内容：" + message.CONTENT;
           

            //保存到数据库
            string photopath = ViewState["imgpath"] != null ? ViewState["imgpath"].ToString() : "";
            if (photopath != "")
            {
                PhotoManager PM = new PhotoManager();
                PHOTO photo = new PHOTO();
                photo.PHOTOPATH = photopath;
                photo.LAST_UPDATER = MyStateManager.Name;
                photo.LAST_UPDATE_TIME = decimal.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
                photo.DOMAINID = MyStateManager.DomainID;

                PM.Save(photo);
                message.PHOTOID = PM.Load(photo.LAST_UPDATER, photo.LAST_UPDATE_TIME).ID.ToString();//获取图片ID
            }

           
            message.Save();
            string hiduer = Hidden1.Value;
            Message_Receive mr = null;
            long LAST_UPDATE_TIME= long.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
            messManager = new MessageManager();

            var mes = messManager.LoadMessage(message.DOMAINID, message.LAST_UPDATER, message.LAST_UPDATE_TIME);
            if (mes == null)
                return;
            
            //循环保存接收人
            foreach (string s in hiduer.Split(','))
            {
                mr = new Message_Receive();
                mr.LAST_UPDATE_TIME = LAST_UPDATE_TIME;
                mr.CREATETIME = mr.LAST_UPDATE_TIME;
                mr.MESSAGEID = mes.ID;
                mr.PERSONID = int.Parse(s);
                mr.READFLAG = false;
                mr.ACTIVE = true;
                mr.DOMAINID = message.DOMAINID;
                mr.ALERTFLAG = "0";
                mr.LAST_UPDATER = MyStateManager.Name;
                mr.Save();

            }
            message.Connection.CommitTransaction();
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
              Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
              this.MyStateManager.UserID, LogContent);
            ShowMessage(SUCCESS, NEW_PAGE);
           
            //return;
        }
        catch (Exception ex)
        {
          
            Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
              Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
              this.MyStateManager.UserID, "消息发送 - 异常:" + ex.Message);
            ShowMessage(ERROR+ex.Message);
            return;
        }
    }





 

    private bool UploadImage()
    {
        if (FileUpload1.HasFile)
        {
            string filetype = "jpg,png,bmp";
            string oldfilename = FileUpload1.FileName;//上载文件名
            string ext = oldfilename.Substring(oldfilename.LastIndexOf(".") + 1).ToLower();//上载文件扩展名
            if (filetype.Contains(ext))
            {
                string datapath = DateTime.Now.ToString("yyyyMMdd");
                string newfilename = Server.MapPath("~/UploadImages/") + datapath;

                if (!System.IO.Directory.Exists(newfilename))
                {
                    System.IO.Directory.CreateDirectory(newfilename);
                }
                string name = BusinessLogic.getNewImageName() + "." + ext;
                FileUpload1.SaveAs(newfilename + "/" + name);
                ViewState["imgpath"] = datapath + "/" + name;
               
               
            }
            else
            {
                
                return false;
            }

        }
        return true;
    }


  
}
